//
//  MTGuideViewController.m
//  ECarService
//
//  Created by ASSASSIN on 10/30/15.
//  Copyright © 2015 cdyqkj. All rights reserved.
//

#import "MTGuideViewController.h"
//#import "HomePageViewController.h"

//#import "MTGuideViewModel.h"

@interface MTGuideViewController ()

@end

@implementation MTGuideViewController

@synthesize animating = _animating;

@synthesize pageScroll = _pageScroll;
/**
 *  初始化方法
 */
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.view.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

#pragma mark -

- (CGRect)onscreenFrame
{
    return [UIScreen mainScreen].bounds;
}

- (CGRect)offscreenFrame
{
    CGRect frame = [self onscreenFrame];
    switch ([UIApplication sharedApplication].statusBarOrientation)
    {
        case UIInterfaceOrientationPortrait:
            frame.origin.y = frame.size.height;
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            frame.origin.y = -frame.size.height;
            break;
        case UIInterfaceOrientationLandscapeLeft:
            frame.origin.x = frame.size.width;
            break;
        case UIInterfaceOrientationLandscapeRight:
            frame.origin.x = -frame.size.width;
            break;
    }
    return frame;
}
/**
 *  展示引导页
 */
- (void)showGuide
{
    if (!_animating && self.view.superview == nil)
    {
        [MTGuideViewController sharedGuide].view.frame = [self offscreenFrame];
        [[self mainWindow] addSubview:[MTGuideViewController sharedGuide].view];
        
        _animating = YES;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.4];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(guideShown)];
        [MTGuideViewController sharedGuide].view.frame = [self onscreenFrame];
        [UIView commitAnimations];
    }
}

- (void)guideShown
{
    _animating = NO;
}
/**
 *  隐藏引导页
 */
- (void)hideGuide
{
    
//    [self dismissViewControllerAnimated:YES completion:nil];
    
    if (!_animating && self.view.superview != nil)
    {
        _animating = YES;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.4];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(guideHidden)];
        [MTGuideViewController sharedGuide].view.frame = [self offscreenFrame];
        [UIView commitAnimations];
    }
}

- (void)guideHidden
{
    _animating = NO;
    [[[MTGuideViewController sharedGuide] view] removeFromSuperview];
}

- (UIWindow *)mainWindow
{
    UIApplication *app = [UIApplication sharedApplication];
    if ([app.delegate respondsToSelector:@selector(window)])
    {
        return [app.delegate window];
    }
    else
    {
        return [app keyWindow];
    }
}

+ (void)show
{
    [[MTGuideViewController sharedGuide].pageScroll setContentOffset:CGPointMake(0.f, 0.f)];
    [[MTGuideViewController sharedGuide] showGuide];
}

+ (void)hide
{
    [[MTGuideViewController sharedGuide] hideGuide];
}

#pragma mark -sharedGuide

+ (MTGuideViewController *)sharedGuide
{
    @synchronized(self)
    {
        static MTGuideViewController *sharedGuide = nil;
        if (sharedGuide == nil)
        {
            sharedGuide = [[self alloc] init];
        }
        return sharedGuide;
    }
}

- (void)pressCheckButton:(UIButton *)checkButton
{
    [checkButton setSelected:!checkButton.selected];
}

- (void)pressEnterButton:(UIButton *)enterButton
{
    [self hideGuide];
    
//    HomePageViewController *homeVC = [[HomePageViewController alloc] init];
//    UINavigationController *naviVC = [[UINavigationController alloc] initWithRootViewController:homeVC];
//    
//    [UIApplication sharedApplication].keyWindow.rootViewController = naviVC;
    
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加控件
    [self addUI];
}
- (void)addUI
{
    NSArray *imageNameArray = [NSArray arrayWithObjects:@"引导页1",@"引导页1",@"引导页1", nil];
    
    //通过 viewModel 拿到数据
//    MTGuideViewModel *guideViewModel = [[MTGuideViewModel alloc] init];
//    
//    [guideViewModel setBlockWithReturnBlock:^(id returnValue) {
    
        //  NSArray *imageURLArray = returnValue;
        
//    } WithErrorBlock:^(id errorCode) {
//        
//    } WithFailureBlock:^{
//        
//    }];
    
    _pageScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _pageScroll.bounces = NO;
    _pageScroll.delegate = self;
    _pageScroll.clipsToBounds = YES;
    self.pageScroll.pagingEnabled = YES;
    self.pageScroll.contentSize = CGSizeMake(self.view.frame.size.width * imageNameArray.count, self.view.frame.size.height);
    [self.view addSubview:self.pageScroll];
    
    _pageControl = [[UIPageControl alloc] init];
    _pageControl.bounds = CGRectMake(0, 0, 150, 30);
    _pageControl.center = CGPointMake(self.view.center.x, 567 * (YYSScreenH / 667));
    //    _pageControl.backgroundColor = [UIColor redColor];
    _pageControl.numberOfPages = imageNameArray.count;
    _pageControl.pageIndicatorTintColor = [UIColor grayColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
    
    [self.view addSubview:_pageControl];
    
    
    NSString *imgName = nil;
    UIView *view;
    for (int i = 0; i < imageNameArray.count; i++) {
        imgName = [imageNameArray objectAtIndex:i];
        view = [[UIView alloc] initWithFrame:CGRectMake((self.view.frame.size.width * i), 0.f, self.view.frame.size.width, self.view.frame.size.height)];
        view.backgroundColor = [UIColor colorWithPatternImage:[self imageResize :[UIImage imageNamed:imgName] andResizeTo:view.frame.size]];
        view.userInteractionEnabled = NO;
        [self.pageScroll addSubview:view];
        
        if (i == imageNameArray.count - 1) {
            
            UIButton *enterButton = [[UIButton alloc] initWithFrame:CGRectMake(0.f, 0.f, 175.f, 35.f)];
            enterButton.layer.borderWidth = 1;
            enterButton.layer.borderColor = [UIColor whiteColor].CGColor;
            enterButton.layer.masksToBounds = YES;
            enterButton.backgroundColor = [UIColor colorWithWhite:0.950 alpha:1.000];
            enterButton.layer.cornerRadius = 5.0f;
            [enterButton setTitle:@"进入蕃莱坞" forState:UIControlStateNormal];
            [enterButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            [enterButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
            [enterButton setCenter:CGPointMake(self.view.center.x + self.view.frame.size.width * (imageNameArray.count-1), 527 * (YYSScreenH / 667))];
            [enterButton addTarget:self action:@selector(pressEnterButton:) forControlEvents:UIControlEventTouchUpInside];
            [self.pageScroll addSubview:enterButton];
        }
    }
}
#pragma mark -scrollView代理
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    NSInteger count = scrollView.contentOffset.x/self.view.frame.size.width;
    _pageControl.currentPage = count;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/**
*   图片压缩
 */
-(UIImage *)imageResize :(UIImage*)img andResizeTo:(CGSize)newSize
{
    CGFloat scale = [[UIScreen mainScreen]scale];
    
    //UIGraphicsBeginImageContext(newSize);
    UIGraphicsBeginImageContextWithOptions(newSize, NO, scale);
    [img drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

@end
